﻿using System;
using System.Collections;

/// <summary>
/// 一个数据包
/// </summary>
public class Packet
{
    private long m_Id;
    public long id => m_Id;
    public const string Head = "||00";

    public static int s_headBytesCount => 4;
    /*{
        get
        {
            byte[] headBytes = System.Text.Encoding.UTF8.GetBytes(Head);
                
            return headBytes.Length;
        }
    }*/
    
    static int s_packetLengthCount
    {
        get
        {
            int length = 0;
            byte[] lenBytes = BitConverter.GetBytes(length);
            return lenBytes.Length;
        }
    }
    public static int s_packetHeadCount
    {
        get
        {
            return s_headBytesCount + s_packetLengthCount;
        }
    }

    public static bool CheckHeadData(byte[] data)
    {
        if (data.Length < s_headBytesCount)
            return false;

        byte[] h = new byte[s_headBytesCount];
        Array.Copy(data, h, h.Length);
        string headTsr = System.Text.Encoding.UTF8.GetString(h);

//        GameDebug.Log($"解析字符串是：{headTsr}   head:{BitConverter.ToString(System.Text.Encoding.UTF8.GetBytes(Head))}   s_headBytesCount:{s_headBytesCount}");
        if (headTsr.Equals(Head))
        {
            //string indexStr = headTsr.Substring(2, 2);
            return true;
        }
        else
        {
            return false;
        }
    }

    protected byte[] m_Data;
    public byte[] data => m_Data;

    protected int m_Index;
    public int dataLengthNeed => contentLength - m_Index;

    protected int m_ContentLength;
    public int contentLength => m_ContentLength;

    protected bool m_ReceiveEnd;
    public bool receiveEnd => m_ReceiveEnd;

    public Packet()
    {
       // m_Id = id;
    }



    public virtual byte[] OnReceiveHead(byte[] data)
    {  
        byte[] h = new byte[s_headBytesCount];
        Array.Copy(data, h, h.Length);
        string headTsr = System.Text.Encoding.UTF8.GetString(h);
        
        int lengthByteLength = s_packetHeadCount - s_headBytesCount;

        var lengthBytes = new byte[lengthByteLength];

        Array.Copy(data, s_headBytesCount, lengthBytes, 0,lengthByteLength);

        m_ContentLength = BitConverter.ToInt32(lengthBytes, 0);

        try
        {
            m_Data = new byte[m_ContentLength];
        }
        catch (Exception e)
        {
            GameDebug.LogError($"m_ContentLength:{m_ContentLength}  headTsr:{headTsr}");
            GameDebug.LogException(e);
        }
        m_Index = 0;
        m_ReceiveEnd = false;

        byte[] contentData = new byte[data.Length - s_packetHeadCount];

        Array.Copy(data, s_packetHeadCount, contentData, 0, contentData.Length);
        byte[] remain =  AddData(contentData);

        return remain;
    }

    public byte[] AddData(byte[] newData)
    {
        byte[] remain = null;
        
        if (newData.Length + m_Index > m_Data.Length)
        {
            remain = new byte[newData.Length + m_Index - m_Data.Length];
         
            Array.Copy(newData, newData.Length - remain.Length, remain, 0 , remain.Length);
            
            //GameDebug.LogError($"AddData Error: {newData.Length} + {m_Index} > {m_Data.Length}");
            
            Array.Copy(newData, 0, m_Data, m_Index, m_Data.Length - m_Index);
            
            m_Index += m_Data.Length - m_Index;
        }
        else
        {
            Array.Copy(newData, 0, m_Data, m_Index, newData.Length);
            
            m_Index += newData.Length;
        }

        if (m_Index >= this.m_ContentLength)
        {
            // GameDebug.Log("接收完数据");
            m_ReceiveEnd = true;
        }

        return remain;
    }


    public override string ToString()
    {
        return $"contentLength{this.contentLength}  dataLength：{data.Length}";
    }





}